﻿$(document).ready(function () {
    $("#ul-nav li:not(.nav-header)").eq(5).addClass("active");
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/admin/auction.ashx?action=",
    popU: null,

    init: function () {
        page.getList();
    },

    getList: function (pageIndex) {
        var title = $.trim($("#title").val());
        var spstatus = $("#spstatus").val();

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        $.get(page.url + "getList", { pageIndex: page.pageIndex, pageSize: page.pageSize, spstatus: spstatus, title: title, start: $("#start").val(), end: $("#end").val(), r: Math.random()
        }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("auctionlist", data));
            page.pager(data.count);
            page.list = data.list;
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.getList
        });
    },

    tStatus: function (status, start, end) {
        if (status == 1)
            return "已结束";
        var now = new Date();
        if (start.ToDate() > now)
            return "未开始";
        if (start.ToDate() <= now && end.ToDate() >= now)
            return "进行中";
        return "未扣素币";
    },

    showResult: function (id) {
        $.get(page.url + "showResult", { auctionId: id, r: Math.random() }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            for (var i = 0; i < page.list.length; i++) {
                if (page.list[i].Id == id) {
                    data.current = page.list[i];
                    break;
                }
            }

            page.popU = page.popU || new PopU();
            page.popU.initPop({
                type: 'string',
                title: "竞拍结果",
                contents: TrimPath.processDOMTemplate("auctionItemList", data),
                w: 900,
                y: $(window).scrollTop() + 20,
                drag: true
            });
            page.popU.showPop();
        }, "json");
    },

    charge: function (id) {
        if (confirm("您确定扣除本场拍卖最终拍卖者的素币么？\r\n假如存在红色标识，表示该拍卖者已经不够支付拍卖的物品，但您的操作将继续扣除他的素币")) {
            $.post(page.url + "charge", { auctionId: id }, function (data) {
                if (data.error) {
                    alert(data.error);
                    return false;
                }
                page.popU.close();
                page.getList();
            }, "json");
        }
    },

    edit: function (id) {
        page.ajaxUploadObj = null;
        var auction = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                auction = page.list[i];
                break;
            }
        }

        if (auction == null) {
            alert("数据异常，请刷新页面后再试");
            return false;
        }

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "编辑竞拍基本信息",
            contents: TrimPath.processDOMTemplate("detail", auction),
            w: 750,
            drag: true,
            y: $(window).scrollTop() + 30
        });
        page.popU.showPop();

        $("#spnDescription").html('<textarea id="udescription" class="span7" placeholder="描述" type="text" required rows="5">' + auction.Description + '</textarea>');

        $("#btnEdit").unbind("click").click(function () {
            page.editAuction(auction.Id);
        });

        page.initUpload();
    },

    initUpload: function () {
        page.ajaxUploadObj = new AjaxUpload('upload', {
            action: "/handler/upload.ashx?action=uploadFile",
            autoSubmit: true,
            data: {},
            onSubmit: function (file, ext) {
                $("#btnAdd").unbind("click").click(function () { alert("Logo上传中，请等图片上传后再操作"); });
                this.disable();
            },
            onChange: function (file, ext) {
                $("#ulogo").val(file);
                this.enable();
            },
            responseType: 'json',
            onComplete: function (file, response) {
                if (response.error) {
                    alert(response.error);
                    $("#ulogo").val('');
                }
                else {
                    $("#ulogo").val(response.url);
                }
                $("#btnAdd").unbind("click").click(page.submit);
                this.enable();
            }
        });
    },

    editAuction: function (id) {
        var title = $.trim($("#utitle").val());
        if (title == "") {
            alert("请输入主题");
            $("#utitle").focus();
            return false;
        }

        var description = $.trim($("#udescription").val());
        if (description == "") {
            alert("请输入描述");
            $("#udescription").focus();
            return false;
        }

        var start = $("#ustart").val();
        if ($.trim(start) == "") {
            alert("请选择开始时间");
            $("#ustart").focus();
            return false;
        }

        var end = $("#uend").val();
        if ($.trim(end) == "") {
            alert("请选择结束时间");
            $("#uend").focus();
            return false;
        }

        var logo = $("#ulogo").val();
        if ($.trim(logo) == "") {
            alert("请输入logo url地址或者上传本地图片");
            $("#ulogo").focus();
            return false;
        }

        $.post(page.url + "edit", { id: id, title: title, description: description, logo: logo, start: start, end: end }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getList();
            page.popU.close();
        }, "json");
    },

    manageItem: function (id) {
        page.ajaxUploadObj = null;

        var data = {};
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                data.current = page.list[i];
                break;
            }
        }

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "物品列表",
            contents: TrimPath.processDOMTemplate("editList", data),
            w: 1000,
            y: $(window).scrollTop() + 20,
            drag: true
        });
        page.popU.showPop();

        $("#spnDescription").html('<textarea id="udescription" placeholder="描述" type="text" required rows="5"></textarea>');

        $("#uprice").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");

        page.initUpload();
        page.showItemList(id);
    },

    showItemList: function (id) {
        $.get(page.url + "getItemList", { auctionId: id, r: Math.random() }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.itemList = data.list;
            $("#itemList").html(TrimPath.processDOMTemplate("itemTemplate", data));
        }, "json");
    },

    addItem: function (auctionId, itemId) {
        var title = $.trim($("#utitle").val());
        if (title == "") {
            alert("请输入物品名称");
            $("#utitle").focus();
            return false;
        }

        var description = $.trim($("#udescription").val());
        if (description == "") {
            alert("请输入描述");
            $("#udescription").focus();
            return false;
        }

        var logo = $("#ulogo").val();
        if ($.trim(logo) == "") {
            alert("请输入图片 url地址或者上传本地图片");
            $("#ulogo").focus();
            return false;
        }

        var price = $("#uprice").val();
        if ($.trim(logo) == "") {
            alert("请输入物品价格");
            $("#uprice").focus();
            return false;
        }
        if (itemId == undefined)
            itemId = 0;
        $.post(page.url + "updateItem", { auctionId: auctionId, itemId: itemId, title: title, description: description, logo: logo, price: price }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.showItemList(auctionId);
            page.cancelEditItem();
        }, "json");
    },

    editItem: function (itemId, auctionId) {
        var title = $.trim($("#utitle").val());
        var description = $.trim($("#udescription").val());
        var logo = $("#ulogo").val();
        var price = $("#uprice").val();

        var noSave = true;
        if (title != "" || description != "" || logo != "" || price != "") {
            noSave = false;
        }
        if (noSave || confirm("右侧的编辑尚未提交，点击确认将丢失这些信息，您确定继续编辑么？")) {
            for (var i = 0; i < page.itemList.length; i++) {
                if (page.itemList[i].Id == itemId) {
                    $("#utitle").val(page.itemList[i].Title);
                    $("#udescription").val(page.itemList[i].Description);
                    $("#ulogo").val(page.itemList[i].Image);
                    $("#uprice").val(page.itemList[i].Price);

                    $("#btnItemA").hide();
                    $("#btnItemB").show().unbind("click").click(function () { page.addItem(auctionId, itemId) });
                    $("#btnItemC").show();
                    break;
                }
            }
        }
    },

    cancelEditItem: function () {
        $('.form-horizontal input:text').val('');
        $("#udescription").val('');
        $("#btnItemA").show();
        $("#btnItemB").hide();
        $("#btnItemC").hide();
    },

    deleteItem: function (itemId, auctionId) {
        if (confirm("您确定删除当前物品么？")) {
            $.post(page.url + "deleteItem", { itemId: itemId, auctionId: auctionId }, function (data) {
                if (data.error) {
                    alert(data.error);
                    return false;
                }
                page.showItemList(auctionId);
            }, "json");
        }
    }
};

